namespace NSpring.Logging.EventFormatters {

using System;
using System.Text;

using NSpring.Logging;
using NSpring.Logging.Common;
using NSpring.Logging.DataFormatters;

public class SimpleEventFormatter : EventFormatter {
    private static string[] yearStrings                 = Constants.YearStrings;
    private static string[] dateStrings                 = Constants.DateStrings;
    private static string[] millisecondStrings          = Constants.MillisecondStrings;

    private const char COLON_CHAR                       = ':';
    private const char HYPHEN_CHAR                      = '-';
    private const char PERIOD_CHAR                      = '.';
    private const char SPACE_CHAR                       = ' ';
    private static string NEW_LINE                      = new String(new char[] {(char)13, (char)10});

    public SimpleEventFormatter() {
        dataFormatter = new FlatDataFormatter();
        suffix = NEW_LINE;
    }
    
    public override string Format(Event _event) {
        StringBuilder sb = new StringBuilder(128);
        int year = _event.Timestamp.Year;
        if (year < yearStrings.Length) {
            sb.Append(yearStrings[year]).Append(HYPHEN_CHAR);
        }
        else {
            sb.Append(year.ToString()).Append(HYPHEN_CHAR);
        }
        sb.Append(dateStrings[_event.Timestamp.Month]).Append(HYPHEN_CHAR);
        sb.Append(dateStrings[_event.Timestamp.Day]).Append(SPACE_CHAR);
        sb.Append(dateStrings[_event.Timestamp.Hour]).Append(COLON_CHAR);
        sb.Append(dateStrings[_event.Timestamp.Minute]).Append(COLON_CHAR);
        sb.Append(dateStrings[_event.Timestamp.Second]).Append(PERIOD_CHAR);
        sb.Append(millisecondStrings[_event.Timestamp.Millisecond]);
        
        sb.Append(SPACE_CHAR);

        if (_event.Message != null) {
            sb.Append(SPACE_CHAR).Append(_event.Message);
        }
        
        if (_event.Data != null) {
            sb.Append(SPACE_CHAR).Append(dataFormatter.Format(_event.Data));
        }
        
        sb.Append(suffix);
        
        return sb.ToString();
    }
    
}
}
